package com.zpc.transform;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author : zpc20 //作者
 * @version : 1.0 // 版本
 * @ClassName : Server  //类名
 * @Description : 服务端  //描述
 * @Date: 2024-11-24  上午 11:10
 */
public class Server {
    public static void main(String[] args) {
        try (// 监听8081端口
             ServerSocket serverSocket = new ServerSocket(8081);
        ) {
            System.out.println("Server running... at port: 8081");

            // 等待客户端的连接
            Socket socket = serverSocket.accept();
            System.out.println("客户端已连接，IP地址为: " + socket.getInetAddress().getHostAddress());
            // 获取socket输入流
            System.out.println("读取客户端数据: " + socket.getPort());
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            System.out.println(reader.readLine());

            OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream());
            writer.write("收到消息");
            writer.flush();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
